package com.sds.sweng.swat.miplatform.ui.design.parts.area.sub;

import java.util.List;

import org.eclipse.draw2d.FlowLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.gef.EditPolicy;

import com.sds.sweng.swat.miplatform.ui.design.layouts.FlowLayoutEx;
import com.sds.sweng.swat.miplatform.ui.design.models.area.sub.LinkButtonSubArea;
import com.sds.sweng.swat.miplatform.ui.design.models.component.MButton;
import com.sds.sweng.swat.miplatform.ui.design.policies.NonCreateFlowLayoutEditPolicy;

public class LinkButtonSubAreaPart extends SubAreaPart {

	protected IFigure createFigure() {
		Label f = (Label) super.createFigure();
		drawChildren(f);

		return f;
	}

	private void drawChildren(Label parent) {
		FlowLayoutEx layout = new FlowLayoutEx();
		layout.setMajorAlignment(FlowLayout.ALIGN_LEFTTOP);
		layout.setMinorSpacing(LinkButtonSubArea.GAP);
		parent.setLayoutManager(layout);

		LinkButtonSubArea area = (LinkButtonSubArea) getModel();
		List<MButton> buttons = area.getButtons();

		if (buttons.size() > 0) {
			parent.setText("");
		} else {
			parent.setText(area.getName());
		}
	}

	protected void createEditPolicies() {
		super.createEditPolicies();

		installEditPolicy(EditPolicy.LAYOUT_ROLE, new NonCreateFlowLayoutEditPolicy());
	}

	protected void refreshVisuals() {
		drawChildren((Label) getFigure());
	}

}
